package com.jinzw.jform.rows

import android.content.Context
import android.graphics.Color
import android.support.v7.widget.AppCompatCheckBox
import android.text.SpannableString
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.EditText
import android.widget.RelativeLayout
import android.widget.Toast

import com.jinzw.jform.core.DimensionUtils

/**
 * Created by jin on 18-2-10.
 */
class ProtocolRow(context: Context) : AbstractTextRow<Boolean>(context) {
    var checkbox: CheckBox? = null
    var value: Boolean = false
    var protocolName: String = ""


    override fun value(): Boolean? {
        return value;
    }

    fun customizeCheckBox(block: ((ProtocolRow, CheckBox) -> Unit)) {
        this.customizeCheckBox = block
    }

    protected var customizeCheckBox: ((ProtocolRow, CheckBox) -> Unit) = { row, editText ->
        editText.text = ""
//        editText.textSize = 16f


    }
    protected var jianting: (() -> Unit) = {}

    fun onJianTing(ting: () -> Unit) {
        this.jianting = ting

    }

    init {
        onCreateView<ProtocolRow> {
            val layout = RelativeLayout(context)

            onCreateCheckView()

            val checkBoxLayoutParams = if (isEvenly) {
                RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, DimensionUtils.dpToPx(evenlyWith))
            } else {
                RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            }
            checkBoxLayoutParams.leftMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_LEFT)
            checkBoxLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL)
            checkBoxLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT)
            checkbox?.layoutParams = checkBoxLayoutParams
            checkbox?.id = 9438498
            layout.addView(checkbox)

            createTitleView()
            val titleLayoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
//            titleLayoutParams.leftMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_LEFT)
            titleLayoutParams.topMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_TOP)
            titleLayoutParams.bottomMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_BOTTOM)
            titleLayoutParams.rightMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_RIGHT)
            titleLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL)
            titleLayoutParams.addRule(RelativeLayout.RIGHT_OF, checkbox?.id!!)
//            titleLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
            var text = SpannableString("我已阅读《${protocolName}》")
            var colorSpan = ForegroundColorSpan(Color.parseColor("#F23232"))
            var a = xieyi()
            text.setSpan(a, 5, protocolName.length + 5, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
            text.setSpan(colorSpan, 4, protocolName.length + 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
            titleView?.movementMethod = LinkMovementMethod.getInstance()
            titleView?.text = text

            titleView?.layoutParams = titleLayoutParams
            layout.addView(titleView)

            layout
        }
    }

    fun onCreateCheckView(): CheckBox {
        checkbox = AppCompatCheckBox(context)
        checkbox!!.setOnCheckedChangeListener { compoundButton, b ->
            value = b
        }
        customizeCheckBox.invoke(this, this.checkbox as CheckBox)

        return checkbox as CheckBox
    }

    override fun rulewen(): Pair<Boolean, String> {
        if (checkbox?.isChecked!!){
            return Pair(true,"请同意$protocolName")
        }
        return super.rulewen()
    }
    inner class xieyi : ClickableSpan() {
        override fun onClick(p0: View?) {
            jianting.invoke()
        }

    }
}